package com.liujit.pay.wx.kit;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.liujit.pay.wx.domain.result.OrderResult;
import com.liujit.pay.wx.domain.result.order.*;
import com.liujit.pay.wx.enums.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 订单查询工具类
 * @author: liujun
 * @create: 2021-02-22 16:08
 */
public class OrderKit {

    /**
     * 获取请求参数
     *
     * @return
     */
    public static OrderResult getResult(JSONObject respJson) {
        OrderResult result = new OrderResult();
        // 应用ID
        result.setAppId(respJson.getStr("appid"));
        // 直连商户号
        result.setMchId(respJson.getStr("mchid"));
        // 商户订单号
        result.setOutTradeNo(respJson.getStr("out_trade_no"));
        // 微信支付订单号
        String transactionId = respJson.getStr("transaction_id");
        if (StrUtil.isNotBlank(transactionId)) {
            result.setTransactionId(transactionId);
        }
        // 交易类型
        String tradeType = respJson.getStr("trade_type");
        if (StrUtil.isNotBlank(tradeType)) {
            result.setTradeType(tradeType);
            result.setTradeTypeName(TradeType.getName(tradeType));
        }
        // 交易状态
        result.setTradeState(respJson.getStr("trade_state"));
        result.setTradeStateName(TradeState.getName(result.getTradeState()));
        // 交易状态描述
        result.setTradeStateDesc(respJson.getStr("trade_state_desc"));
        // 付款银行
        String bankType = respJson.getStr("bank_type");
        if (StrUtil.isNotBlank(bankType)) {
            result.setBankType(bankType);
            result.setBankTypeName(BankType.getName(bankType));
        }
        // 附加数据
        String attach = respJson.getStr("attach");
        if (StrUtil.isNotBlank(attach)) {
            result.setAttach(attach);
        }
        // 支付完成时间
        String successTime = respJson.getStr("success_time");
        if (StrUtil.isNotBlank(successTime)) {
            result.setSuccessTime(successTime);
        }
        // 支付者
        result.setOrderPayer(getPayer(respJson.getJSONObject("payer")));
        // 订单金额
        JSONObject amount = respJson.getJSONObject("amount");
        if (amount != null) {
            result.setOrderAmount(getAmount(amount));
        }
        // 场景信息
        JSONObject sceneInfo = respJson.getJSONObject("scene_info");
        if (sceneInfo != null) {
            result.setOrderSceneInfo(getSceneInfo(sceneInfo));
        }
        // 优惠功能
        JSONObject promotionDetail = respJson.getJSONObject("promotion_detail");
        if (promotionDetail != null) {
            result.setOrderPromotionDetail(getPromotionDetail(promotionDetail));
        }
        return result;
    }

    /**
     * 获取支付者
     *
     * @param payer
     * @return
     */
    private static OrderPayer getPayer(JSONObject payer) {
        OrderPayer result = new OrderPayer();
        // 用户标识
        result.setOpenId(payer.getStr("openid"));
        return result;
    }

    /**
     * 获取订单金额
     *
     * @param amount
     * @return
     */
    private static OrderAmount getAmount(JSONObject amount) {
        OrderAmount result = new OrderAmount();
        // 总金额
        Integer total = amount.getInt("total");
        if (total != null) {
            result.setTotal(total);
        }
        // 用户支付金额
        Integer payerTotal = amount.getInt("payer_total");
        if (payerTotal != null) {
            result.setPayerTotal(payerTotal);
        }
        // 货币类型
        String currency = amount.getStr("currency");
        if (StrUtil.isNotBlank(currency)) {
            result.setCurrency(currency);
        }
        // 用户支付币种
        String payerCurrency = amount.getStr("payer_currency");
        if (StrUtil.isNotBlank(payerCurrency)) {
            result.setPayerCurrency(payerCurrency);
        }
        return result;
    }

    /**
     * 获取场景信息
     *
     * @param sceneInfo
     * @return
     */
    private static OrderSceneInfo getSceneInfo(JSONObject sceneInfo) {
        OrderSceneInfo result = new OrderSceneInfo();
        // 商户端设备号
        String deviceId = sceneInfo.getStr("device_id");
        if (StrUtil.isNotBlank(deviceId)) {
            result.setDeviceId(deviceId);
        }
        return result;
    }

    /**
     * 获取优惠功能
     *
     * @param promotionDetail
     * @return
     */
    private static OrderPromotionDetail getPromotionDetail(JSONObject promotionDetail) {
        OrderPromotionDetail result = new OrderPromotionDetail();
        // 券ID
        result.setCouponId(promotionDetail.getStr("coupon_id"));
        // 优惠名称
        String name = promotionDetail.getStr("name");
        if (StrUtil.isNotBlank(name)) {
            result.setName(name);
        }
        // 优惠范围
        String scope = promotionDetail.getStr("scope");
        if (StrUtil.isNotBlank(scope)) {
            result.setScope(scope);
            result.setScopeName(PromotionScope.getName(result.getScope()));
        }
        // 优惠类型
        String type = promotionDetail.getStr("type");
        if (StrUtil.isNotBlank(type)) {
            result.setType(type);
            result.setTypeName(PromotionType.getName(result.getType()));
        }
        // 优惠券面额
        result.setAmount(promotionDetail.getInt("amount"));
        // 活动ID
        String stockId = promotionDetail.getStr("stock_id");
        if (StrUtil.isNotBlank(stockId)) {
            result.setStockId(stockId);
        }
        // 微信出资
        Integer wechatPayContribute = promotionDetail.getInt("wechatpay_contribute");
        if (wechatPayContribute != null) {
            result.setWechatPayContribute(wechatPayContribute);
        }
        // 商户出资
        Integer merchantContribute = promotionDetail.getInt("merchant_contribute");
        if (merchantContribute != null) {
            result.setMerchantContribute(merchantContribute);
        }
        // 其他出资
        Integer otherContribute = promotionDetail.getInt("other_contribute");
        if (otherContribute != null) {
            result.setOtherContribute(otherContribute);
        }
        // 优惠币种
        String currency = promotionDetail.getStr("currency");
        if (StrUtil.isNotBlank(currency)) {
            result.setCurrency(currency);
        }
        // 商品列表
        JSONArray goodsDetailArray = promotionDetail.getJSONArray("goods_detail");
        if (goodsDetailArray != null) {
            List<OrderGoodsDetail> orderGoodsDetailList = new ArrayList<>();
            for (int i = 0; i < goodsDetailArray.size(); i++) {
                JSONObject goodsDetail = goodsDetailArray.getJSONObject(i);
                OrderGoodsDetail orderGoodsDetail = new OrderGoodsDetail();
                // 商品编码
                orderGoodsDetail.setGoodsId(goodsDetail.getStr("goods_id"));
                // 商品数量
                orderGoodsDetail.setQuantity(goodsDetail.getInt("quantity"));
                // 商品单价
                orderGoodsDetail.setUnitPrice(goodsDetail.getInt("unit_price"));
                // 商品优惠金额
                orderGoodsDetail.setDiscountAmount(goodsDetail.getInt("discount_amount"));
                // 商品备注
                String goodsRemark = goodsDetail.getStr("goods_remark");
                if (StrUtil.isNotBlank(goodsRemark)) {
                    orderGoodsDetail.setGoodsRemark(goodsRemark);
                }
                orderGoodsDetailList.add(orderGoodsDetail);
            }
            result.setOrderGoodsDetailList(orderGoodsDetailList);
        }
        return result;
    }
}
